// 导入 mongoose 模块
const mongo = require("mongoose");

// 连接数据库
mongo.connect("mongodb://127.0.0.1:27017/bilibili");

// 设置回调
mongo.connection.once("open", () => {
  let bookSchema = new mongo.Schema({
    name: String,
    author: String,
    price: Number,
    is_hot: Boolean,
    tags: Array,
    pub_time: Date,
    test: mongo.Schema.Types.Mixed,
  });

  // 创建模型对象
  let bookModel = mongo.model("books", bookSchema);

  // 新增文档
  bookModel.create(
    {
      name: "三国演义",
      author: "罗贯中",
      price: 50,
      is_hot: true,
      tags: ["鬼怪", "励志", "社会"],
      pub_time: new Date(),
      test: "正确的方针和政策需要坚持不懈的执行力",
    },
    /* (err, data) => {
      if (err) {
        console.log(err);
        return;
      }
      console.log(data);
      mongo.disconnect();
    }, */
  );
  console.log("创建成功");
});

mongo.connection.on("error", () => {
  console.log("连接失败");
});

mongo.connection.on("close", () => {
  console.log("连接关闭");
});
